package com.ocgs.dao.impl;

import com.ocgs.base.core.exception.BaseDAOException;
import com.ocgs.base.util.StringUtil;
import com.ocgs.dao.CustomerDAO;
import com.ocgs.mapper.CustomerMapper;
import com.ocgs.model.Customer;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bryant
 * Date: 10/21/12
 * Time: 3:40 PM
 * To change this template use File | Settings | File Templates.
 */
public class CustomerDAOImpl implements CustomerDAO {

    private SqlSessionTemplate sqlSessionTemplate;

    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    
    public int deleteByPrimaryKey(Integer id) throws BaseDAOException{

        try{
            return sqlSessionTemplate.getMapper(CustomerMapper.class).deleteByPrimaryKey(id);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    
    public int insert(Customer record) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(CustomerMapper.class).insert(record);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }

    }
    public int insertSelective(Customer record) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(CustomerMapper.class).insert(record);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    
    public Customer selectByPrimaryKey(Integer id) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(CustomerMapper.class).selectByPrimaryKey(id);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    
    public int updateByPrimaryKeySelective(Customer record) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(CustomerMapper.class).updateByPrimaryKeySelective(record);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    
    public int updateByPrimaryKey(Customer record) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(CustomerMapper.class).updateByPrimaryKey(record);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    public List<Customer> findAllCustomer(Customer customer) throws BaseDAOException{
        try{
            if(customer!=null){
                Customer search = new Customer();
                search.setCustomerName(StringUtil.escapeSQLString(customer.getCustomerName()));
                search.setMobileNo(StringUtil.escapeSQLString(customer.getMobileNo()));
                search.setTelephoneNo(StringUtil.escapeSQLString(customer.getTelephoneNo()));
                search.setMunicipioId(customer.getMunicipioId());
                search.setTownId(customer.getTownId());
                search.setVillageId(customer.getVillageId());
                return sqlSessionTemplate.getMapper(CustomerMapper.class).findAllCustomer(search);
            } else return null;

        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

    public String retrieveCustomerAddress(Customer record) throws BaseDAOException{
        try{
            return sqlSessionTemplate.getMapper(CustomerMapper.class).retrieveCustomerAddress(record);
        }catch (Exception e){
            throw new BaseDAOException(e);
        }
    }

//    public List<customer> getAllCustomer() throws BaseDAOException{
//        try{
//            return sqlSessionTemplate.getMapper(CustomerMapper.class).getAllCustomer();
//        }catch (Exception e){
//            throw new BaseDAOException(e);
//        }
//    }
//
//    public List<customer> searchStorage(customer customer) throws BaseDAOException{
//        try{
//            return sqlSessionTemplate.getMapper(CustomerMapper.class).findAllStorage(customer);
//        }catch (Exception e){
//            throw new BaseDAOException(e);
//        }
//    }
}
